package edu.hut.mpdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hut.mpdemo.domain.User;
import edu.hut.mpdemo.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {

    //  //总提示@Autowired有问题,改成Resource就行了
    @Resource
    UserMapper userMapper;

//    //或者改成这种方式
//    UserMapper userMapper;
//    @Autowired
//    public void setUserDao (UserMapper userMapper) {
//        this.userMapper = userMapper;
//    }


    @RequestMapping(value = "/hello")

    public String hello(Model model) {
//        列出所有
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
        System.out.println("-----------------------------------------------");
        model.addAttribute("userList", userList);

//        构造QueryWrapper
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        其他地方，每次使用之前要先.clear()一下
        queryWrapper.eq("name", "jack").between("age", 1, 50);
        userList = userMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
        System.out.println("-----------------------------------------------");

        User user = new User();
        user.setAge(10);
        user.setEmail("haha");
        user.setName("zhangsan");

//        添加
        userMapper.insert(user);
        System.out.println("-----------------------------------------------");

//        删除
        queryWrapper.clear();
        queryWrapper.eq("name", "zhangsan");
        int i = userMapper.delete(queryWrapper);
        System.out.println("-----------------------------------------------");
        i = userMapper.deleteById(2L);
//        System.out.println(i);
        System.out.println("-----------------------------------------------");

//        更新
        user.setId(1L);
        i = userMapper.updateById(user);
        System.out.println(i);
        System.out.println("-----------------------------------------------");

//        分页，要更改UserMapper文件
        Page<User> page = new Page<>(1, 2);
        userList = userMapper.iPageSelect(page);
        userList.forEach(System.out::println);
        System.out.println("-----------------------------------------------");

//        分页，结合queryWrapper使用
        queryWrapper.clear();
        queryWrapper.eq("name", "tom");
        IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
        userList = iPage.getRecords();
        userList.forEach(System.out::println);
        System.out.println("当前页面" + iPage.getPages());

        return "hello";
    }

    @RequestMapping("/checkbox")
    public String checkBox(Model model){
        ArrayList<String> l1 = new ArrayList<>();
        l1.add("english");
        l1.add("math");
        model.addAttribute("l1", l1);
        ArrayList<String> l2 = new ArrayList<>();
        l2.add("english");
        l2.add("chinese");
        model.addAttribute("l2", l2);
        return "checkbox";
    }

    //    设置起始页面
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    //返回json数据示例
    @RequestMapping("/json")
    @ResponseBody
    public User json() {
        User user = new User();
        user.setAge(19);
        System.out.println(user);
        return user;
    }


}
